import { Injectable } from '@angular/core'
import { AdEvent } from 'monk-design/utils'

export type LoadingEventOptions = [string[], string, 'add' | 'remove']

@Injectable({
  providedIn: 'root',
})
export class AdLoadingService {
  public static LOADING_CHANGE: string = 'ad_loading_state_change'
  private loadingState: string[] = []
  constructor() {}
  public addLoadingState(state: string = 'ad_default_loading_state') {
    this.loadingState.push(state)
    AdEvent.$sent<LoadingEventOptions>(
      AdLoadingService.LOADING_CHANGE,
      this.loadingState,
      state,
      'add'
    )
  }
  public removeLoadingState(state: string = 'ad_default_loading_state') {
    const index = this.loadingState.findIndex((item) => item === state)
    if (index === -1) {
      return console.error('[AdLoadingService.removeLoadingState]查询loadingState失败', state)
    }
    this.loadingState.splice(index, 1)
    AdEvent.$sent(AdLoadingService.LOADING_CHANGE, this.loadingState, state, 'remove')
  }
}
